import time

import os

from iyinyue import settings


def save_file(f, des):
    """ save file from request.FILES"""
    if not f: return None

    filename = time.strftime('%Y%m%d%H%M%S') + str(f)
    fullpath = os.path.join(settings.MEDIA_ROOT, des, filename)
    with open(fullpath, 'wb+') as destination:
        for chunk in f.chunks():
            destination.write(chunk)
    destination.close()
    return (destination, filename)


def save_data(data, des, name):
    if not data: return None

    filename = time.strftime('%Y%m%d%H%M%S') + str(name)
    fullpath = os.path.join(settings.MEDIA_ROOT, des, filename)
    with open(fullpath, 'wb+') as destination:
        destination.write(data)
    destination.close()
    return (destination, filename)